const api = require('../api/api');
const util = require('../utils/util');

exports.pageInterceptor = function (pageObj, flag = false) {
  if (flag && api.log.enable) {
    // let _onShow = pageObj.onShow;
    pageObj.onShow = function () {
      // let that = this;
      let user = wx.getStorageSync('userInfo');
      if (!util.isEmpty(user)) {
        let pageInfo = {
          openid: user.openid,
          url: this.route
        };

        // 调用页面访问接口
        savePageVisitLog(pageInfo);

        // 展示页面
        // _onShow.call(that);
      }
    }
  }
  return Page(pageObj);
};

/**
 * 添加页面访问日志
 */
function savePageVisitLog(pageInfo) {
  wx.request({
    url: api.log.prefix + api.log.url.savePageVisitLog,
    header: {
      'content-type': 'application/json'
    },
    data: pageInfo,
    method: 'post',
    success: function (res) { }
  });
}